package com.treasure.mall.biz.entity.community;


import com.alibaba.fastjson.JSONArray;
import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.zbq.common.ienum.community.CommunityEnums;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 社区内容
 */
@Entity
@Table(name = "zb_community_article")
@Setter
@Getter
public class CommunityArticlePO extends BasePO {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 社区标题
     */
    @Column(name = "title", length = 30)
    private String title;

    /**
     * 社区内容
     */
    @Column(name = "description", columnDefinition = "longtext")
    private String description;

    /**
     * 作者
     */
    @ManyToOne
    @JoinColumn(name = "rel_user_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private UserPO author;
    /**
     * 社区图片列表
     */
    @Column(name = "images", columnDefinition = "longtext")
    private String picList;

    /**
     * 社区封面图
     */
    @Column(name = "cover_url", length = 200)
    private String mainPic;

    /**
     * 视频地址
     */
    @Column(name = "video_url", length = 200)
    private String videoUrl;

    /**
     * 审核状态
     */
    @Column(name = "audit_status")
    private CommunityEnums.AuditStatus auditStatus;

    /**
     * 审核理由
     */
    @Column(name = "reason", length = 200)
    private String reason;

    /**
     * 喜欢总数量
     */
    @Column(name = "like_count")
    private long likeCount;

    /**
     * 访问量
     */
    @Column(name = "visit_count")
    private long visitCount;

    /**
     * 评论数量
     */
    @Column(name = "comment_count")
    private long commentCount;


    /**
     * 社区内容类型
     * {@link com.treasure.mall.zbq.common.ienum.community.CommunityEnums.ContentType}
     */
    @Column(name = "content_type")
    private CommunityEnums.ContentType contentType = CommunityEnums.ContentType.IMAGE_TEXT;

    public CommunityArticlePO() {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public List<String> getImages() {
        if (StringUtils.isNotBlank(this.picList)) {
            return JSONArray.parseArray(this.picList, String.class);
        }
        return new ArrayList<>();
    }
}
